/*-----------------------------------------
file name  : fxp_add.v
created    : 2025/04/26 14:08:13
modified   : 2025-04-26 14:17:24
description: additon 
notes      : 
author     : yyrwkk
-----------------------------------------*/
module fxp_add # (
    parameter WIIA = 8  ,
    parameter WIFA = 8  ,
    parameter WIIB = 8  ,
    parameter WIFB = 8  ,
    parameter WOI  = 8  ,
    parameter WOF  = 8  ,
    parameter ROUND= 1  
)(
    input  [WIIA+WIFA-1:0] ina     ,
    input  [WIIB+WIFB-1:0] inb     ,
    output [WOI +WOF -1:0] out     ,
    output                 overflow
);

localparam WII = WIIA>WIIB ? WIIA : WIIB;
localparam WIF = WIFA>WIFB ? WIFA : WIFB;
localparam WRI = WII + 1                ;
localparam WRF = WIF                    ;

wire [WII+WIF-1:0] inaz;
wire [WII+WIF-1:0] inbz;

wire [WRI+WRF-1:0] res ;
assign res = $signed(inaz) + $signed(inbz);

// convert ina
// use more bits -> can not overflow,and not truncate
fxp_zoom # (
    .WII      ( WIIA     ),
    .WIF      ( WIFA     ),
    .WOI      ( WII      ),
    .WOF      ( WIF      ),
    .ROUND    ( 0        )
) ina_zoom (
    .in       ( ina      ),
    .out      ( inaz     ),
    .overflow (          )
);

// convert inb 
// use more bits -> can not overflow,and not truncate
fxp_zoom # (
    .WII      ( WIIB     ),
    .WIF      ( WIFB     ),
    .WOI      ( WII      ),
    .WOF      ( WIF      ),
    .ROUND    ( 0        )
) inb_zoom (
    .in       ( inb      ),
    .out      ( inbz     ),
    .overflow (          )
);

// convert out
fxp_zoom # (
    .WII      ( WRI      ),
    .WIF      ( WRF      ),
    .WOI      ( WOI      ),
    .WOF      ( WOF      ),
    .ROUND    ( ROUND    )
) res_zoom (
    .in       ( res      ),
    .out      ( out      ),
    .overflow ( overflow )
);

endmodule
